namespace Webbness.Generator.CodeDom.Sql

import System
import System.Text
import Neo.Generator.Core
import Neo.MetaModel

class SqlConstraint:	

	_rel as EntityRelationship
	_constraintName as string
	_updateRule as string
	_deleteRule as string
	_comments as List
	
	def constructor():
		_comments = List()
	
	def constructor(rel as EntityRelationship):
		self()
		_rel = rel
		_updateRule = rel.UpdateRule.ToString()
		_deleteRule = rel.DeleteRule.ToString()
		_foreignTable = rel.LocalEntity.TableName
		_foreignlKey = rel.LocalKey
		_localKey = rel.ForeignKey
		_localTable = rel.ForeignTableName
		_varName = rel.VarName
	
	def AddComment(comment as string):
		_comments.Add(comment)
	
	SqlComments as string:
		get:
			return _comments.Join(' ')
	
	[property(LocalTable)]
	_localTable as string
	
	[property(LocalKey)]
	_localKey as string
	
	[property(ForeignTable)]
	_foreignTable as string
	
	[property(ForeignKey)]
	_foreignlKey as string
	
	[property(Action)]
	_action as ActionType
	
	UpdateRule as string:
		get:
			if String.IsNullOrEmpty(_updateRule):
				return "NO ACTION"
			if _updateRule.Equals("none", StringComparison.InvariantCultureIgnoreCase):
				return "NO ACTION"
			return _updateRule
		set:
			_updateRule = value
			
	DeleteRule as string:
		get:
			if String.IsNullOrEmpty(_deleteRule):
				return "NO ACTION"
			if _deleteRule.Equals("none", StringComparison.InvariantCultureIgnoreCase):
				return "NO ACTION"
			return _deleteRule
		set:
			_deleteRule = value
	
	[property(VarName)]
	_varName as string
	
	ConstraintName as string:
		get:
			if _constraintName:
				return _constraintName
			else:
				return "fk_${_foreignTable}_${_varName}"
		set:
			_constraintName = value
